/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import BD.Seccion;
import BD.Usuario;
import DigaFoil.Controlador;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;

/**
 *
 * @author ARMGARCES
 */
public class PantallaAdmin extends javax.swing.JFrame implements Pantalla{
    private boolean sesion;
    private String imagen = java.util.ResourceBundle.getBundle("GUI/fondos").getString("fondoPpal");
    /**
     * Creates new form VentanaPpal
     */
    public PantallaAdmin() {   
        initComponents();
                
        setTitle("DIGA C.A. - DIGAFOIL #2");
        Toolkit toolkit = Toolkit.getDefaultToolkit();
        Dimension screenSize = toolkit.getScreenSize();
        this.setSize(screenSize.width,screenSize.height);

        int x = 0;
        int y = 0;
        this.setLocation(x, y);
        
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentHidden(ComponentEvent e) {
                salir();
                ((JFrame)(e.getComponent())).dispose();
            }
        });
        //JPanel jp = new JPanel
        //ImagePanel panel = new ImagePanel(new ImageIcon("images/background.png").getImage());
    }
    
    /**
     * 
     */
    @Override
    public void salir(){   
        Gui.getInstance().pantallaPpal();
        Gui.getInstance().updateSesion(); 
        this.dispose();
    }

    /**
     * 
     * @param _user 
     */
    @Override
    public void updateSesion(Usuario _user){
        if(_user != null && _user.getActivo()){
            l_sesion.setText(_user.toString());
            b_sesion.setText("Cerrar Sesion");
            sesion = false;
        }else{
            this.salir();
        }
        panelSesion.repaint();
           
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        fondo = new JPanelConFondo(imagen);
        panelSesion = new javax.swing.JPanel();
        b_sesion = new javax.swing.JButton();
        l_sesion = new javax.swing.JLabel();
        addUsuario = new javax.swing.JButton();
        addLugar = new javax.swing.JButton();
        b_categ_mp = new javax.swing.JButton();

        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        fondo.setMinimumSize(new java.awt.Dimension(1024, 800));
        fondo.setPreferredSize(new java.awt.Dimension(800, 600));

        panelSesion.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Sesion", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 10))); // NOI18N

        b_sesion.setText("Iniciar Sesion");
        b_sesion.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        b_sesion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                b_sesionActionPerformed(evt);
            }
        });

        l_sesion.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        l_sesion.setText("Usuario:  ");
        l_sesion.setMaximumSize(null);
        l_sesion.setMinimumSize(null);
        l_sesion.setPreferredSize(null);

        javax.swing.GroupLayout panelSesionLayout = new javax.swing.GroupLayout(panelSesion);
        panelSesion.setLayout(panelSesionLayout);
        panelSesionLayout.setHorizontalGroup(
            panelSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(panelSesionLayout.createSequentialGroup()
                .addGroup(panelSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(panelSesionLayout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(l_sesion, javax.swing.GroupLayout.DEFAULT_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(panelSesionLayout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addComponent(b_sesion)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        panelSesionLayout.setVerticalGroup(
            panelSesionLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelSesionLayout.createSequentialGroup()
                .addComponent(l_sesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(b_sesion)
                .addContainerGap())
        );

        b_sesion.getAccessibleContext().setAccessibleDescription("");

        addUsuario.setText("Agregar Usuario");
        addUsuario.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addUsuarioActionPerformed(evt);
            }
        });

        addLugar.setText("Agregar Lugar");
        addLugar.setMaximumSize(new java.awt.Dimension(125, 23));
        addLugar.setMinimumSize(new java.awt.Dimension(125, 23));
        addLugar.setPreferredSize(new java.awt.Dimension(125, 23));
        addLugar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addLugarActionPerformed(evt);
            }
        });

        b_categ_mp.setText("Agregar Categoria MP");

        javax.swing.GroupLayout fondoLayout = new javax.swing.GroupLayout(fondo);
        fondo.setLayout(fondoLayout);
        fondoLayout.setHorizontalGroup(
            fondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fondoLayout.createSequentialGroup()
                .addGroup(fondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(fondoLayout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(panelSesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(fondoLayout.createSequentialGroup()
                        .addGap(108, 108, 108)
                        .addComponent(addUsuario)
                        .addGap(18, 18, 18)
                        .addComponent(addLugar, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(b_categ_mp)
                        .addGap(0, 495, Short.MAX_VALUE)))
                .addContainerGap())
        );
        fondoLayout.setVerticalGroup(
            fondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fondoLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(panelSesion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(127, 127, 127)
                .addGroup(fondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addUsuario)
                    .addComponent(addLugar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(b_categ_mp))
                .addContainerGap(564, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fondo, javax.swing.GroupLayout.DEFAULT_SIZE, 1024, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(fondo, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void b_sesionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_sesionActionPerformed
        // TODO add your handling code here:
        //
        boolean cerrarSesion = Controlador.getInstance().cerrarSesion();
    }//GEN-LAST:event_b_sesionActionPerformed

    private void addUsuarioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addUsuarioActionPerformed
        // TODO add your handling code here:
        //
        Usuario _user = Controlador.getInstance().getUser();
        if(_user != null && _user.getActivo()){
            Gui.getInstance().addUsuario();
        }
    }//GEN-LAST:event_addUsuarioActionPerformed

    private void addLugarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addLugarActionPerformed
        // TODO add your handling code here:
        //
        Usuario _user = Controlador.getInstance().getUser();
        if(_user != null && _user.getActivo()){
            Gui.getInstance().addLugar();
        }

    }//GEN-LAST:event_addLugarActionPerformed

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addLugar;
    private javax.swing.JButton addUsuario;
    private javax.swing.JButton b_categ_mp;
    private javax.swing.JButton b_sesion;
    private javax.swing.JPanel fondo;
    private javax.swing.JLabel l_sesion;
    private javax.swing.JPanel panelSesion;
    // End of variables declaration//GEN-END:variables

    /**
     * 
     */
    @Override
    public void habilitarSecciones() {
        List<Seccion> secciones = new ArrayList<Seccion>();
        secciones = Controlador.getInstance().getUser().getFkidRol().getSeccionesHabilitadas();     
        // SECCIONES A SER HABILITADAS SEGUN EL ROL DE USUARIO
        
        for(Seccion seccion : secciones){
            //SECCION DE ADD USUARIO
            if(seccion.getNombre().equals("addUsuario")){
                this.addUsuario.setEnabled(true);
            }
        }
    }

    /**
     * 
     */
    @Override
    public void deshabilitarSecciones() {
        this.addUsuario.setEnabled(false);
    }
}
